/***************************************************************************
 *  -- Copyright Bit Armory Labs 2007
 *
 * FILE:     TemplateFactory.cs, 35 code lines, [0.99 KB]
 * DATE:     8/26/2007 5:44 PM
 * MODIFIED: 8/29/2007 5:22 AM
 * PATH:     C:\Programming\Projects\Slinks\Slinks.Resources
 *
 * NAMESPACE: Slinks.Generator
 * CLASS:     TemplateFactory
 *
 * AUTHOR:   Brian Chavez (Cowboy), bchavez@bitarmory.com
 *
 *                        Licence
 *
 * This library, code generator, and source code
 * is licensed under the terms and conditions defined in the GNU Public
 * License. You are free to use it for non-commercial purposes.
 * If you are going to use any part of it in a commercial 
 * application, contact me and we can work something out.
 * You may use the code generated output of Slink and Slink
 * as a development tool in both commercial and 
 * non-commercial applications.
 *
 * THE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS", WITHOUT WARRANTY
 * OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THIS
 * SOFTWARE.
 ***************************************************************************/

using System;
using System.Reflection;
using TaHoGen;
using TaHoGen.Generators;
namespace Slink.Generator
{
    public static class TemplateFactory
    {
        /// <summary>
        /// Get template type
        /// </summary>
        /// <returns>Type</returns>
        public static Type GetTemplateGeneratorType( string template )
        {
            Assembly templateAssembly = TemplateCompiler.Compile( template );
            if( templateAssembly == null )
            {
                throw new Exception( "Error creating template assembly." );
            }
            
            //Get the template type.
            Type templateType = null;
            try
            {
                templateType = templateAssembly.GetTypes()[0];
            }
            catch( ReflectionTypeLoadException rtle )
            {

            }
            
            if( templateType == null )
            {
                throw new Exception( "Error creating template type." );
            }
            return templateType;
        }

        /// <summary>
        /// Create text generator
        /// </summary>
        /// <param name="template">Template</param>
        /// <param name="pt">Point</param>
        /// <returns>Text generator</returns>
        public static TextGenerator CreateTextGenerator( Type textGeneratorType, PropertyTable pt )
        {

            //Instantiate the template and assign properties at the same time.
            TextGenerator textGenerator = Activator.CreateInstance( textGeneratorType ) as TextGenerator;

            if( textGenerator == null )
            {
                throw new Exception( "Error creating " + textGeneratorType.ToString() + " text generator!" );
            }

            textGenerator.LoadProperties( pt );

            return textGenerator;
        }
    }
}